package ui;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import midlet.VadeMecumMIDlet;
import multimedia.SaveSnapshotThread;
import multimedia.SendToServiceWithContextThread;
import multimedia.SendToServiceWithoutContextThread;

public class VadeMecumImageViewer extends Canvas {
	
	private byte[] imageArray = null;
	
	private VadeMecumMIDlet midlet = null;
	
	//commands
	public Command save = null;
	public Command sendToServiceWhithoutContext = null;
	public Command sendToServiceWithContext = null;
	public Command back = null;
	
	public VadeMecumImageViewer(VadeMecumMIDlet midlet, byte[] imageArray) {
		this.midlet = midlet;
		
		save = new Command("Save picture", Command.OK, 1);
		sendToServiceWhithoutContext = new Command("Send without context", Command.OK, 1);
		sendToServiceWithContext = new Command("Send with context", Command.OK, 1);
		back = new Command("Back", Command.BACK, 1);
		
		addCommand(save);
		addCommand(sendToServiceWhithoutContext);
		addCommand(sendToServiceWithContext);
		addCommand(back);
		
		this.imageArray = imageArray;
	}

	protected void paint(Graphics graphic) {
		//create an image
		Image image = Image.createImage(imageArray, 0, imageArray.length);		
		graphic.drawImage(image, 0, 0, Graphics.LEFT | Graphics.TOP);
	}

	public void saveSnapshot() {
		Thread saveThread = new Thread(new SaveSnapshotThread(midlet, this, imageArray, System.currentTimeMillis()));
		saveThread.start();
	}

	public void sendWithoutContext() {
		Thread sendThread = new Thread(new SendToServiceWithoutContextThread(midlet, this, imageArray));
		sendThread.start();
	}

	public void sendWithContext() {
		Thread sendThread = new Thread(new SendToServiceWithContextThread(midlet, this, imageArray));
		sendThread.start();
	}
	
}
